﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;
using ModelLayer;
using ShopKart.Common;
using ShopKart.Controllers;

namespace ShopKart.Areas.User.Controllers
{
    public class OrderController : BaseController
    {
        // GET: User/Order
        public ActionResult Index()
        {
            return View("~/Areas/User/Views/Order/Index.cshtml");
        }

        [HttpPost]
        [AuthUser(Roles = "User")]
        public ActionResult PlaceOrder()
        {
            bool result = false;
            UserCartManager userCartManager = new UserCartManager();
            OrderManager orderManager = new OrderManager();
            var userCarts = userCartManager.GetByUser(SessionObject.UserId);
            if (userCarts.Count() > 0)
            {
                var order = new Order()
                {
                    OrderDate = DateTime.Now,
                    DeliveryDate = DateTime.Now.AddDays(7),
                    UserId = SessionObject.UserId,
                    OrderDetails = userCarts.Select(userCart => new OrderDetail()
                    {
                        ProductId = userCart.ProductId,
                        Quantity = 1,
                    }).ToList()
                };
                result = orderManager.AddUpdate(order);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}